using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace TankWars
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        #region Textures and fonts
        private SpriteFont font;
        private Texture2D statusAreaBorderTexture;

        private Texture2D TankTexture;
        private Dictionary<Type, Texture2D> dicShotTextures = new Dictionary<Type, Texture2D>();
        private Dictionary<Type, Texture2D> dicShotIcons = new Dictionary<Type, Texture2D>();

        //Field textures.
        private Texture2D blockTexture;

        private Texture2D mapTexture;
        #endregion

        private Player player;
        private Player testPlayer;

        private TankManager tankManager;
        private GameField field;

        //private Texture2D mousePointer;
        private Vector2 mousePosition;
        private MouseState mouseState;

        private Map gameMap;

        private StatusArea statusArea;

        //private AudioEngine audioEngine;
        //private WaveBank waveBank;
        //private SoundBank soundBank;



        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            //sound initialization -- This points to the XACT project, sound bank, and wave bank bank file.
            //audioEngine = new AudioEngine("Content\\Audio\\TankWarsAudio.xgs");
            //waveBank = new WaveBank(audioEngine, "Content\\Audio\\Wave Bank.xwb");
            //soundBank = new SoundBank(audioEngine, "Content\\Audio\\Sound Bank.xsb");


            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
            //Load sprite fonts
            this.font = Content.Load<SpriteFont>("unispace12");

            #region Load all textures
            this.statusAreaBorderTexture = Content.Load<Texture2D>("StatusAreaBorder");

            this.TankTexture = Content.Load<Texture2D>("Tanks/tank3base");

            //Add game field textures
            this.blockTexture = Content.Load<Texture2D>("block");
            field = new GameField(this.blockTexture, this.spriteBatch);

            //Load all shot textures
            this.dicShotTextures.Add(typeof(Weapons.Bullet), Content.Load<Texture2D>("Weapons/BulletSmall"));
            this.dicShotTextures.Add(typeof(Weapons.PlasmaBall), Content.Load<Texture2D>("Weapons/PlasmaBallSmall"));
            this.dicShotTextures.Add(typeof(Weapons.Laser), Content.Load<Texture2D>("Weapons/LaserSmall"));
            this.dicShotTextures.Add(typeof(Weapons.Missile), Content.Load<Texture2D>("Weapons/MissileSmall"));
            this.dicShotTextures.Add(typeof(Weapons.Mine), Content.Load<Texture2D>("Weapons/MineSmall"));

            //Load all shot icons
            this.dicShotIcons.Add(typeof(Weapons.Bullet), Content.Load<Texture2D>("Weapons/Bullet"));
            this.dicShotIcons.Add(typeof(Weapons.PlasmaBall), Content.Load<Texture2D>("Weapons/PlasmaBall"));
            this.dicShotIcons.Add(typeof(Weapons.Laser), Content.Load<Texture2D>("Weapons/Laser"));
            this.dicShotIcons.Add(typeof(Weapons.Missile), Content.Load<Texture2D>("Weapons/Missile"));
            this.dicShotIcons.Add(typeof(Weapons.Mine), Content.Load<Texture2D>("Weapons/Mine"));

            this.mapTexture = Content.Load<Texture2D>("Maps/Level1");
            //this.mapTexture = Content.Load<Texture2D>("map1");
            #endregion

            // TODO: use this.Content to load your game content here
            this.tankManager = new TankManager(this.TankTexture, this.Content.Load<Texture2D>("Tanks/tank3turret") ,this.dicShotTextures, this.dicShotIcons, this.spriteBatch);
            this.player = new Player("Nick");
            this.tankManager.NewTank(this.player);
            this.tankManager.dicTanks[this.player].vPosition = new Vector2(700.0f, 1000.0f);

            this.testPlayer = new Player("test");
            this.tankManager.NewTank(this.testPlayer, Color.Red);
            this.tankManager.dicTanks[this.testPlayer].vPosition = new Vector2(1200.0f, 1000.0f);

            this.statusArea = new StatusArea(this.statusAreaBorderTexture, this.font, this.tankManager.dicTanks[this.player], this.graphics.GraphicsDevice.Viewport);
            this.graphics.GraphicsDevice.Viewport = this.statusArea.ResizeViewport(this.graphics.GraphicsDevice.Viewport);

            //this.mousePointer = Content.Load<Texture2D>("Mouse");

            //soundBank.PlayCue("MENU");

            this.gameMap = new Map(this.mapTexture, this.tankManager.dicTanks[this.player], this.graphics.GraphicsDevice.Viewport);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            this.GetKeyboardInput(gameTime);
            this.tankManager.Update(gameTime, Mouse.GetState(), this.gameMap);

            this.tankManager.checkFieldCollisions(field);

            //this.MoveMouse();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            //Always draw map first
            this.gameMap.Draw(this.spriteBatch);

            this.field.drawField(this.gameMap);
            this.tankManager.Draw(this.gameMap);
            this.statusArea.Draw(this.spriteBatch, this.graphics.GraphicsDevice, Mouse.GetState());
            base.Draw(gameTime);
            //this.spriteBatch.Begin(SpriteBlendMode.Additive);
            //this.spriteBatch.Draw(mousePointer, mousePosition, Color.White);
            //this.spriteBatch.End();
        }

        /// <summary>
        /// Gets the keyboard input and moves the player's tank in the proper direction
        /// </summary>
        private void GetKeyboardInput(GameTime gameTime)
        {
            KeyboardState keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Left) || keyboard.IsKeyDown(Keys.A))
            {
                this.tankManager.dicTanks[this.player].turnLeft();
            }
            else if (keyboard.IsKeyDown(Keys.Right) || keyboard.IsKeyDown(Keys.D))
            {
                this.tankManager.dicTanks[this.player].turnRight();
            }

            if (keyboard.IsKeyDown(Keys.Up) || keyboard.IsKeyDown(Keys.W))
            {
                this.tankManager.dicTanks[this.player].accelerate();
            }
            else if (keyboard.IsKeyDown(Keys.Down) || keyboard.IsKeyDown(Keys.S))
            {
                this.tankManager.dicTanks[this.player].reverse();
            }
            #region Check direction
            /*
            if (keyboard.IsKeyDown(Keys.Up))
            {
                if (keyboard.IsKeyDown(Keys.Down))
                {
                    if (keyboard.IsKeyDown(Keys.Left))
                    {
                        if (keyboard.IsKeyDown(Keys.Right))
                        {
                            this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.Stopped;
                        }
                        else
                        {
                            this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.Left;
                        }
                    }
                    else if (keyboard.IsKeyDown(Keys.Right))
                    {
                        this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.Right;
                    }
                    else
                    {
                        this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.Stopped;
                    }
                }
                else if (keyboard.IsKeyDown(Keys.Left))
                {
                    if (keyboard.IsKeyDown(Keys.Right))
                    {
                        this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.Up;
                    }
                    else
                    {
                        this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.UpLeft;
                    }
                }
                else if (keyboard.IsKeyDown(Keys.Right))
                {
                    this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.UpRight;
                }
                else
                {
                    this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.Up;
                }
            }
            else if (keyboard.IsKeyDown(Keys.Down))
            {
                if (keyboard.IsKeyDown(Keys.Left))
                {
                    if (keyboard.IsKeyDown(Keys.Right))
                    {
                        this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.Down;
                    }
                    else
                    {
                        this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.DownLeft;
                    }
                }
                else if (keyboard.IsKeyDown(Keys.Right))
                {
                    this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.DownRight;
                }
                else
                {
                    this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.Down;
                }
            }
            else if (keyboard.IsKeyDown(Keys.Left))
            {
                if (keyboard.IsKeyDown(Keys.Right))
                {
                    this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.Stopped;
                }
                else
                {
                    this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.Left;
                }
            }
            else if (keyboard.IsKeyDown(Keys.Right))
            {
                this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.Right;
            }
            else
            {
                this.tankManager.dicTanks[this.player].Direction = Tank.TankDirection.Stopped;
            }
              */
            #endregion

            if(keyboard.IsKeyDown(Keys.Space))
            {
                this.tankManager.dicTanks[this.player].FirePrimary(gameTime);
            }
            if(keyboard.IsKeyDown(Keys.LeftShift) || keyboard.IsKeyDown(Keys.RightShift))
            {
                this.tankManager.dicTanks[this.player].FireSecondary(gameTime);
            }
        }

        private void MoveMouse()
        {
            this.mouseState = Mouse.GetState();
            this.mousePosition.X = this.mouseState.X;
            this.mousePosition.Y = this.mouseState.Y;

            // Restrict the Mouse so that it stays inside the current display
            if (this.mousePosition.X < 0)
                this.mousePosition.X = 0;
            if (this.mousePosition.X > this.Window.ClientBounds.Width)
                this.mousePosition.X = this.Window.ClientBounds.Width;
            if (this.mousePosition.Y < 0)
                this.mousePosition.Y = 0;
            if (this.mousePosition.Y > this.Window.ClientBounds.Height)
                this.mousePosition.Y = this.Window.ClientBounds.Height;
        }
    }
}
